/***********************************************************
** Project    : basic IO programming
** Category   : Tutorial ARM
** MCU        : STM32F103RCT6
** Create Date: 18/3/2013
** Finish Date: 18/3/2013
** Author     : VietComp Electronics
** Email      : support@vietcomp.com.vn
** Website    : www.vietcomp.com.vn
** Developer  : Cuong Chu
** Version    : 1.0
** Revision   : 
** 15-4-2013 New creation
** 22-4-2013 Add code display for setting time
************************************************************/
/***********************************************************
**    INCLUDE
************************************************************/
#include <stm32f10x.h> 
#include "button.h"
#include "rtc.h"
#include "plat_uart.h"
#include "stdio.h"
#include "global.h"
#include "lcd4bit.h"
#include "string.h"
#include "IF_FLASH.h"
/*****************************************************************************/
/* DEFINITION OF CONSTANT                                                    */
/*****************************************************************************/
/* number of channels timer */
#define VP_TIMER_NBR            4
/* number of form object display on LCD */
#define VP_FORM_NBR             4
/*****************************************************************************/
/* DEFINITION OF TYPES                                                       */
/*****************************************************************************/
typedef enum {
	CONFIG_TIMER_ON = 0,
	CONFIG_TIMER_OFF
}ConfigTimerModeType;

typedef enum
{
  DATETIME_BACK,
  DATETIME_SET_DAY,
  DATETIME_SET_MON,
  DATETIME_SET_YEAR,
	DATETIME_SAVE,
  DATETIME_SET_HOUR,
  DATETIME_SET_MIN,
  DATETIME_SET_SEC,
}SettingTypeIdxType;

typedef enum
{
	BUT_1,
	BUT_2,
	BUT_3
}ButtonScanType;

typedef enum {
	CONFIG_TIMER_BACK_SPACE = 0,
  CONFIG_TIMER_ON_OFF,
	CONFIG_TIMER_HOUR,
	CONFIG_TIMER_MIN,
	CONFIG_TIMER_SAVE,
	CONFIG_TIMER_ENABLED
}TimerCfgType;

typedef enum
{
	LINE_BOT = 0,
	LINE_TOP
}LCDLineType;
typedef enum
{
	TITLE_SHOWED = 0,
	TITLE_NOT_SHOW
}FormTitleType;

typedef enum 
{
	BUT1_IS_PRESSED = 0,
	BUT2_IS_PRESSED,
	BUT3_IS_PRESSED,
	NONE_BUTTON_PRESSED,
}ButtonPressedType;


typedef enum 
{
	DISPLAY_MAIN = 0,
	DISPLAY_TIMER_1,
	DISPLAY_TIMER_2,
	DISPLAY_TIMER_3,
}DisplayObjectType;

typedef enum {
  MAIN_STATE,
  MENU_STATE,
	CONFIG_STATE,
  SETTING_TIME_STATE
}MainStateType;

typedef enum {
	TIMER_1,
	TIMER_2,
	TIMER_3
}TimerNumericType;

typedef enum {
	LCD_NOT_PLUG,
	LCD_PLUGED
}LCDDetectType;

typedef enum
{
  TIMER_ON,
  TIMER_OFF,
}TimerStateType;

typedef enum
{
	TIMER_DISABLED,
	TIMER_ENABLED
	
}TimerDeEnType;

typedef struct{
  uint8_t           Min;  /* minute */
  uint8_t           Hour; /* hour */
  TimerStateType    TimerState;
	TimerDeEnType     Enabled;
}TimerConfigType;


typedef struct 
{
	char LineOne[17];
	char LineSecond[17];
	ConfigTimerModeType CfgMode;
}LCDDisplayBufferType;

typedef struct {
	uint8_t X;
	uint8_t Y;
}ConfigLocationType;

typedef struct {
  /* timer config time ON */
  TimerConfigType  TimerConfigON[VP_TIMER_NBR];
  /* timer config OFF */
  TimerConfigType  TimerConfigOFF[VP_TIMER_NBR];

}TimerType;
/*****************************************************************************/
/* DEFINITION OF CONSTANTS                                                   */
/*****************************************************************************/
#define MAIN_TIMEOUT_PULSEFILTER                   10000
/* value of time scanning led us <max 65535 */
#define LED_7SEG_REFRESH_PERIOD                     100        
/******************************************************************************/
/* DECLARATION OF VARIABLES OF MODULE                                         */
/* Declaration of local variables shall have the specifier STATIC             */
/******************************************************************************/
/* system Date Time */
static DateTimeType          DateTime;
/* temp date time for configure */
static DateTimeType          DateTimeCurrent;
static TimerType             Timer;
/* flag title show */
static FormTitleType         TitleShow = TITLE_NOT_SHOW;
/* system machine state */
static MainStateType         mcState = MAIN_STATE;         
/* slect form display */
static DisplayObjectType     DisplayIdx;
/* temp buffer for configure timer */
static TimerConfigType       SettingTempBuffer[VP_FORM_NBR];
/* data to display  on LCD */
static LCDDisplayBufferType  FormObject[VP_FORM_NBR];
/* flag detect LCD is pluged */
static LCDDetectType				 LCDDetected = LCD_NOT_PLUG;
/* location select on LCD */
static TimerCfgType          ConfigLocationIdx = CONFIG_TIMER_BACK_SPACE;
/* select location for setting time */
static SettingTypeIdxType  TimeSettingIdx = DATETIME_BACK;
/* Coordinates of object control on LCD */
static const ConfigLocationType    PointConfig[CONFIG_TIMER_ENABLED+1] = 
{
	{0,0},  /* coordinate of Back button */
  {2,0},  /* Coordinate of switch config ON/OFF timer */
  {6,0},  /* Coordinate of setting hour */
  {9,0},  /* Coordinate of setting minutes*/
  {12,0},  /* Coordinate of Save button */
  {13,1}
}; 
/* Coordinates of object control on LCD for setting time */
static const ConfigLocationType    CoordinateTime[DATETIME_SET_SEC+1] = 
{
  {0,1},
	{8,1},  
  {11,1}, 
  {14,1},
  {0,0},
  {8,0},  
  {11,0},
  {14,0}
} ; 
/* day of week display */
static const char *DayOfWeek[] =
{
  "Sun",
  "Mon",
  "Tue",
  "Wed",
  "Thu",
  "Fri",
  "Sat"
}; 
/* string display timer enable or disable */
static const char *TimerDeEn[] =
{
	"TAT",
	"BAT"
};
/******************************************************************************/
/* DECLARATION OF GLOBAL VARIABLES OF MODULE                                  */
/******************************************************************************/
/* LCD class in module LCD */
LCD_Class LCD;
/******************************************************************************/
/**    DEFINE OF INTERNAL FUNCTION                                            */
/******************************************************************************/
/***********************************************************
** Function Name: VP_CheckTimerTrigger()
** Description  : The function shall check time cfg of Timer module
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_CheckTimerTrigger(void);
/***********************************************************
** Function Name: VP_CheckTrigger()
** Description  : The function shall check on/off trigger
**                 of Timer module
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_CheckTrigger(void);
/***********************************************************
** Function Name: VP_ScanButton()
** Description  : The function scan and return key pressed
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
ButtonPressedType VP_ScanButton(ButtonScanType); 
/***********************************************************
** Function Name: VP_ConvertData()
** Description  : The function shall convert data for LCD display
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_ConvertData(void);
/***********************************************************
** Function Name: VP_ProcessMainState()
** Description  : The function process in main state
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_ProcessMainState(void);
/***********************************************************
** Function Name: VP_ProcessSettingState()
** Description  : The function process in configuration state
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_ProcessSettingState(void);
/***********************************************************
** Function Name: VP_SettingTime()
** Description  : The function process setting time for system
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_SettingTime(void);
/***********************************************************
** Function Name: VP_ConfigTimer()
** Description  : The function setting timer
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_ConfigTimer(void);
/***********************************************************
** Function Name: VP_HardwareInit()
** Description  : The function shall be init hardware system
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_HardwareInit(void);
/***********************************************************
** Function Name: VP_FormShow()
** Description  : The function show object form to LCD
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_FormShow(void);
/***********************************************************
**    Main Function
************************************************************/
int main(void)
{
  /* init hardware */
  VP_HardwareInit();

  printf("\r\nwrite %d \r\n",IF_FLASH_WriteAppConfig ((__IO uint16_t*)&Timer,sizeof (Timer)));
  for(;;)
  {
    switch(mcState)
    {
      /* process in main state */
      case MAIN_STATE:
        VP_ProcessMainState();
      break;
      /* process in listing menu state */
      case MENU_STATE:
        VP_ProcessSettingState();
      break;
			/* config parameter state */
			case CONFIG_STATE:
				VP_ConfigTimer();
      break;
      case SETTING_TIME_STATE:
        /* setting time */
        VP_SettingTime();
			break;
      default: break;
    }
  }
}
/************************************************************
**  EXPAND OF INTERNAL FUNCTION
*************************************************************/
/***********************************************************
** Function Name: VP_HardwareInit()
** Description  : The function shall be init hardware system
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_HardwareInit(void)
{
  InOutTypeDef i = BUT1;
  /* Setup SysTick Timer for 1 msec interrupts */
  if (SysTick_Config(SystemCoreClock / 1000))  
  { 
        /* Capture error */
       __disable_irq();
        NVIC_SystemReset();
        assert_param(0);
  }
	  /* init io port */
  for( ; i < IOm ; i++)
  {
    IO_init(i);
  }
  /* init real time clock */
  RTC_Init();
	/* init serial port */
	COMInit(eSerCOM1,sbaud_115200);
	/* send dummy byte */
	printf("\xff\r\n");
	/* check LCD is pluged */
	if(!IO_read(LCD_DETECT))
	{
		/* set LCD detected flag */
    LCDDetected = LCD_PLUGED;
	}
	else
	{
		/* do nothing */
	}
	/* default config mode display */
	FormObject[DISPLAY_TIMER_1].CfgMode = CONFIG_TIMER_ON;
	FormObject[DISPLAY_TIMER_2].CfgMode = CONFIG_TIMER_ON;
	FormObject[DISPLAY_TIMER_3].CfgMode = CONFIG_TIMER_ON;
}
/***********************************************************
** Function Name: VP_CheckTrigger()
** Description  : The function shall check on/off trigger
**                 of Timer module
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_CheckTrigger(void)
{
	/* TIMER 1 */
	/* is on */
  if(Timer.TimerConfigON[TIMER_1].TimerState == TIMER_ON)
	{
		IO_clr(TRIGGER_1_CHANNEL);
	}
	/* is off */
	else
	{
		IO_set(TRIGGER_1_CHANNEL);
	}
	/* TIMER 2 */
	/* is on */
  if(Timer.TimerConfigON[TIMER_2].TimerState == TIMER_ON)
	{
		IO_clr(TRIGGER_2_CHANNEL);
	}
	/* is off */
	else
	{
		IO_set(TRIGGER_2_CHANNEL);
	}
	/* TIMER 3 */
	/* is on */
  if(Timer.TimerConfigON[TIMER_3].TimerState == TIMER_ON )
	{
		IO_clr(TRIGGER_3_CHANNEL);
	}
	/* is off */
	else
	{
		IO_set(TRIGGER_3_CHANNEL);
	}	
}
/***********************************************************
** Function Name: VP_CheckTimerTrigger()
** Description  : The function shall check time cfg of Timer module
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_CheckTimerTrigger(void)
{
  TimerNumericType i = TIMER_1;
  for(; i < TIMER_3; i ++)
  {
    /* timer is state ON */
    if(Timer.TimerConfigON[i].TimerState == TIMER_ON && Timer.TimerConfigON[i].Enabled == TIMER_ENABLED)
    {
      /* check for state OFF */
      if(DateTime.Hour == Timer.TimerConfigOFF[i].Hour && DateTime.Min == Timer.TimerConfigOFF[i].Min)
      {
        Timer.TimerConfigON[i].TimerState = TIMER_OFF;
        //@todo
      }
      else
      {
        /* do nothing */
      }
    }
    else if (Timer.TimerConfigON[i].TimerState == TIMER_OFF && Timer.TimerConfigON[i].Enabled == TIMER_ENABLED)
    {
      /* check for state OFF */
      if(DateTime.Hour == Timer.TimerConfigON[i].Hour && DateTime.Min == Timer.TimerConfigON[i].Min)
      {
        Timer.TimerConfigON[i].TimerState = TIMER_ON;
        //@todo
      }
      else
      {
        /* do nothing */
      }
    }
		else
		{
			Timer.TimerConfigON[i].TimerState = TIMER_OFF; 
		}
  }
}
/***********************************************************
** Function Name: VP_ProcessMainState()
** Description  : The function process in main state
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_ProcessMainState(void)
{
	char tmp[17] = {0};
	/* check LCD is pluged */
	if(LCD_PLUGED == LCDDetected )
	{
		printf("LCD is plugged\r\n");
		/* init LCD */
		LCDInit();
		/* delay 500 ms for refresh */
		VP_DelayMs(500);
		/* clear screen */
		LCD.ClearScreen();
		/* display main */
    sprintf(tmp,"%s   %02d/%02d/%02d",DayOfWeek[DateTime.Date],DateTime.Day,DateTime.Month,DateTime.Year+2000);
		LCD.Printf(tmp,strlen(tmp),1);
		/* convert time */
		sprintf(tmp,"    %02d:%02d:%02d    ",DateTime.Hour,DateTime.Min,DateTime.Sec);
		/* display to tiem to lcd */
		LCD.Printf(tmp,strlen(tmp),2);
    	/* default config mode display */
    FormObject[DISPLAY_TIMER_1].CfgMode = CONFIG_TIMER_ON;
    FormObject[DISPLAY_TIMER_2].CfgMode = CONFIG_TIMER_ON;
    FormObject[DISPLAY_TIMER_3].CfgMode = CONFIG_TIMER_ON;
    /* form default when LCD is plugged */
    DisplayIdx = DISPLAY_MAIN;
		/* switch state */
		mcState = MENU_STATE;
	}
	else
	{
		/* do nothing */
	}
	/* check Timer config and control */
	VP_CheckTimerTrigger();
	/* check timer is on/off */
	VP_CheckTrigger();
}
/***********************************************************
** Function Name: VP_ProcessSettingState()
** Description  : The function process in setting state
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_ProcessSettingState(void)
{
	uint8_t  RequestDisplay = 0;
  char  tmp[17] ={0};
	/* display to LCD */
  if(RTC_GetFlag())
	{
		RTC_ClearUpdateTimeFlag();
		RTC_GetTime(&DateTime);
		RequestDisplay = 1 ;
	}
	/* read button 1 */
	if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
	{
		/* check and decrement display object */
	  if(DisplayIdx > DISPLAY_MAIN)
		{
			DisplayIdx--;
			RequestDisplay =  1;
			/* set title is not show */
		  TitleShow = TITLE_NOT_SHOW;			
		}
		else
		{
			 /* do nothing */
		}
	}
	else
	{
		/* do nothing */
	}
	/* read button 2 */
	if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
	{
		/* copy data to temp buffer */
		SettingTempBuffer[DISPLAY_MAIN].Hour = DateTime.Hour;
		SettingTempBuffer[DISPLAY_MAIN].Min = DateTime.Min;
		/* copy data of timer 1 to temp */
		if(FormObject[DISPLAY_TIMER_1].CfgMode == CONFIG_TIMER_ON)
		{
			SettingTempBuffer[DISPLAY_TIMER_1].Hour = Timer.TimerConfigON[TIMER_1].Hour;
			SettingTempBuffer[DISPLAY_TIMER_1].Min = Timer.TimerConfigON[TIMER_1].Min;
		}
		else
		{
			SettingTempBuffer[DISPLAY_TIMER_1].Hour = Timer.TimerConfigOFF[TIMER_1].Hour;
			SettingTempBuffer[DISPLAY_TIMER_1].Min = Timer.TimerConfigOFF[TIMER_1].Min;
		}
		/* copy data of timer 2 to temp */
		if(FormObject[DISPLAY_TIMER_2].CfgMode == CONFIG_TIMER_ON)
		{
			SettingTempBuffer[DISPLAY_TIMER_2].Hour = Timer.TimerConfigON[TIMER_2].Hour;
			SettingTempBuffer[DISPLAY_TIMER_2].Min = Timer.TimerConfigON[TIMER_2].Min;
		}
		else
		{
			SettingTempBuffer[DISPLAY_TIMER_2].Hour = Timer.TimerConfigOFF[TIMER_2].Hour;
			SettingTempBuffer[DISPLAY_TIMER_2].Min = Timer.TimerConfigOFF[TIMER_2].Min;
		}
		/* copy data of timer 2 to temp */
		if(FormObject[DISPLAY_TIMER_3].CfgMode == CONFIG_TIMER_ON)
		{
			SettingTempBuffer[DISPLAY_TIMER_3].Hour = Timer.TimerConfigON[TIMER_3].Hour;
			SettingTempBuffer[DISPLAY_TIMER_3].Min = Timer.TimerConfigON[TIMER_3].Min;
		}
		else
		{
			SettingTempBuffer[DISPLAY_TIMER_3].Hour = Timer.TimerConfigOFF[TIMER_3].Hour;
			SettingTempBuffer[DISPLAY_TIMER_3].Min = Timer.TimerConfigOFF[TIMER_3].Min;
		}
    /* check is setting timer or time */
    if(DisplayIdx> DISPLAY_MAIN)
    {
      /* enable curror here */
      LCD_EnableCurcor();
      /* next to state configuration */
      mcState = CONFIG_STATE;
      /* set location of currcor */
      ConfigLocationIdx = CONFIG_TIMER_BACK_SPACE;
			RequestDisplay = 1;
    }
    else
    {
      /* is setting time */
      /* enable curror here */
      LCD_EnableCurcor();   
      /* clear screen */
      LCD.ClearScreen();
      /* display main */
      sprintf(tmp,"BACK    %02d/%02d/%02d",DateTime.Day,DateTime.Month,DateTime.Year);
      LCD.Printf(tmp,strlen(tmp),1);
      /* convert time */
      sprintf(tmp,"SAVE    %02d:%02d:%02d",DateTime.Hour,DateTime.Min,DateTime.Sec);
      /* display to tiem to lcd */
      LCD.Printf(tmp,strlen(tmp),2);
      LCD.SetCursorLocal(CoordinateTime[DATETIME_BACK].X,CoordinateTime[DATETIME_BACK].Y);    
      /* next to state setting time */
      mcState = SETTING_TIME_STATE; 
			/* default location */
			TimeSettingIdx = DATETIME_BACK;
      /* derement time year */
    } 
	}
	else
	{
		/* do nothing */
	}
	/* read button 3 */
	if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
	{
		/* check and decrement display object */
	  if(DisplayIdx < DISPLAY_TIMER_3)
		{
			DisplayIdx++;
			RequestDisplay = 1 ;
			/* set title is not show */
		  TitleShow = TITLE_NOT_SHOW;
		}
		else
		{
			 /* do nothing */
		}
	}
	else
	{
		/* do nothing */
	}
	/* check process display */
  if(RequestDisplay)
	{
		VP_FormShow();
		LCD.SetCursorLocal(PointConfig[CONFIG_TIMER_BACK_SPACE].X,PointConfig[CONFIG_TIMER_BACK_SPACE].Y);    
	}
	else
	{
		/* do nothing */
	}
}
/***********************************************************
** Function Name: VP_ConfigTimer()
** Description  : The function config timer
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_ConfigTimer(void)
{
	uint8_t  RequestDisplay = 0;
	uint8_t  FormReresh = 0;
	uint8_t keepGoing = 0;
  char   tmp[5] = {0};
	/* read button 1 */
	if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
	{
	  /* request display */	
    if(ConfigLocationIdx > CONFIG_TIMER_BACK_SPACE)
		{
			ConfigLocationIdx--;
			RequestDisplay = 1;
		}			
		else
		{
			/* do nothing */
		}
	}
	else
	{
		/* do nothing */
	}
	/* read button 2 */
	if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
	{
		switch(ConfigLocationIdx)
		{
			case CONFIG_TIMER_BACK_SPACE: 
				/* back to state menu selecting */
				mcState = MENU_STATE;
				/* disable curror here */
				LCD_DisableCurcor();
			 //@todo
			break;
			case CONFIG_TIMER_ON_OFF:
				/* switch config mode */
				if(FormObject[DisplayIdx].CfgMode == CONFIG_TIMER_ON)
				{
					/* switch to config value of off */
					FormObject[DisplayIdx].CfgMode = CONFIG_TIMER_OFF;
				}
				else
				{
					/* switch to config on value */
					FormObject[DisplayIdx].CfgMode = CONFIG_TIMER_ON;
				}
				FormReresh = 1;
			break;
			case CONFIG_TIMER_HOUR:
				   keepGoing = 1;
					while(keepGoing)
					{
						if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
						{
							/* check button 1 for decrement value */
							if(SettingTempBuffer[DisplayIdx].Hour == 0)
							{
								SettingTempBuffer[DisplayIdx].Hour = 23;
							}
							else
							{
								SettingTempBuffer[DisplayIdx].Hour--;
							}
							/* convert to string */
							sprintf(tmp,"%02d",SettingTempBuffer[DisplayIdx].Hour);
							/* display to LCD */
							LCD.WriteString(tmp,strlen(tmp));
							/* set curcor back here */
							LCD.SetCursorLocal(PointConfig[ConfigLocationIdx].X,PointConfig[ConfigLocationIdx].Y);  							
						}
						else
						{
							/* do nothing */
						}
						/* exit  */
						if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
						{
							keepGoing = 0;
						}
						else
						{
							/* do nothing */
						}						
						if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
						{
							SettingTempBuffer[DisplayIdx].Hour++;
							/* check button 1 for decrement value */
							if(SettingTempBuffer[DisplayIdx].Hour >23 )
							{
								SettingTempBuffer[DisplayIdx].Hour = 0 ;
							}
							else
							{
								/* do nothing */
							}
							/* convert to string */
							sprintf(tmp,"%02d",SettingTempBuffer[DisplayIdx].Hour);
							/* display to LCD */
							LCD.WriteString(tmp,strlen(tmp));
							/* set curcor back here */
							LCD.SetCursorLocal(PointConfig[ConfigLocationIdx].X,PointConfig[ConfigLocationIdx].Y);							
						}
						else
						{
							/* do nothing */
						}						
					} 
			break;
			case CONFIG_TIMER_MIN:
				  keepGoing = 1;
					while(keepGoing)
					{
						if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
						{
							/* check button 1 for decrement value */
							if(SettingTempBuffer[DisplayIdx].Min == 0)
							{
								SettingTempBuffer[DisplayIdx].Min = 59;
							}
							else
							{
								SettingTempBuffer[DisplayIdx].Min--;
							}
							/* convert to string */
							sprintf(tmp,"%02d",SettingTempBuffer[DisplayIdx].Min);
							/* display to LCD */
							LCD.WriteString(tmp,strlen(tmp));
							/* set curcor back here */
							LCD.SetCursorLocal(PointConfig[ConfigLocationIdx].X,PointConfig[ConfigLocationIdx].Y);  							
						}
						else
						{
							/* do nothing */
						}
						/* exit  */
						if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
						{
							keepGoing = 0;
						}
						else
						{
							/* do nothing */
						}
						if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
						{
							SettingTempBuffer[DisplayIdx].Min++;
							/* check button 1 for decrement value */
							if(SettingTempBuffer[DisplayIdx].Min > 59 )
							{
								SettingTempBuffer[DisplayIdx].Min = 0 ;
							}
							else
							{
								/* do nothing */
							}
							/* convert to string */
							sprintf(tmp,"%02d",SettingTempBuffer[DisplayIdx].Min);
							/* display to LCD */
							LCD.WriteString(tmp,strlen(tmp));
							/* set curcor back here */
							LCD.SetCursorLocal(PointConfig[ConfigLocationIdx].X,PointConfig[ConfigLocationIdx].Y);							
						}
						else
						{
							/* do nothing */
						}						
					}         
			break;		
			case CONFIG_TIMER_SAVE:				
				/* saving data */				
				DateTime.Hour = SettingTempBuffer[DISPLAY_MAIN].Hour ;
				DateTime.Min = SettingTempBuffer[DISPLAY_MAIN].Min ;
				/* copy data of timer 1 to temp */
				if(FormObject[DISPLAY_TIMER_1].CfgMode == CONFIG_TIMER_ON)
				{
					Timer.TimerConfigON[TIMER_1].Hour = SettingTempBuffer[DISPLAY_TIMER_1].Hour ;
					Timer.TimerConfigON[TIMER_1].Min = SettingTempBuffer[DISPLAY_TIMER_1].Min ;
				}
				else
				{
					Timer.TimerConfigOFF[TIMER_1].Hour = SettingTempBuffer[DISPLAY_TIMER_1].Hour ;
					Timer.TimerConfigOFF[TIMER_1].Min = SettingTempBuffer[DISPLAY_TIMER_1].Min ;
				}
				/* copy data of timer 2 to temp */
				if(FormObject[DISPLAY_TIMER_2].CfgMode == CONFIG_TIMER_ON)
				{
					Timer.TimerConfigON[TIMER_2].Hour = SettingTempBuffer[DISPLAY_TIMER_2].Hour ;
					Timer.TimerConfigON[TIMER_2].Min = SettingTempBuffer[DISPLAY_TIMER_2].Min ;
				}
				else
				{
					Timer.TimerConfigOFF[TIMER_2].Hour = SettingTempBuffer[DISPLAY_TIMER_2].Hour ;
					Timer.TimerConfigOFF[TIMER_2].Min = SettingTempBuffer[DISPLAY_TIMER_2].Min ;
				}
				/* copy data of timer 2 to temp */
				if(FormObject[DISPLAY_TIMER_3].CfgMode == CONFIG_TIMER_ON)
				{
					Timer.TimerConfigON[TIMER_3].Hour = SettingTempBuffer[DISPLAY_TIMER_3].Hour ;
					Timer.TimerConfigON[TIMER_3].Min = SettingTempBuffer[DISPLAY_TIMER_3].Min ;
				}
				else
				{
					Timer.TimerConfigOFF[TIMER_3].Hour = SettingTempBuffer[DISPLAY_TIMER_3].Hour ;
					Timer.TimerConfigOFF[TIMER_3].Min = SettingTempBuffer[DISPLAY_TIMER_3].Min ;
				}
				// @todo
        LCD.ClearScreen();
        LCD.Printf(" CAI DAT TIMER  ",16,1);
        LCD.Printf("   THANH CONG   ",16,2);    
        VP_DelayMs(1000);
        ConfigLocationIdx = CONFIG_TIMER_BACK_SPACE;
        RequestDisplay = 1; 
        FormReresh = 1;
			break;
			case CONFIG_TIMER_ENABLED:
				/* check enabled and disable it */
				if(Timer.TimerConfigON[DisplayIdx].Enabled == TIMER_ENABLED)
				{
					/* disable */
					Timer.TimerConfigON[DisplayIdx].Enabled = TIMER_DISABLED;
				}
				else
				{
					/* enabled */
					Timer.TimerConfigON[DisplayIdx].Enabled = TIMER_ENABLED;
				}
				/* set curcor at location enable/disable button */
				LCD.SetCursorLocal(PointConfig[ConfigLocationIdx].X,PointConfig[ConfigLocationIdx].Y);
				/* write string display */
			  LCD.WriteString((char*)TimerDeEn[Timer.TimerConfigON[DisplayIdx].Enabled],3);
				/* set back here */
				LCD.SetCursorLocal(PointConfig[ConfigLocationIdx].X,PointConfig[ConfigLocationIdx].Y);				
			break;
			default: break;
		}
	}
	else
	{
		/* do nothing */
	}
	/* read button 4 */
	if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
	{
	  /* request display */	
    if(ConfigLocationIdx < CONFIG_TIMER_ENABLED)
		{
			ConfigLocationIdx++;
			RequestDisplay = 1;
		}			
		else
		{
			/* do nothing */
		}
	}
	else
	{
		/* do nothing */
	}	
	/* check process display */
  if(RequestDisplay)
	{
		LCD.SetCursorLocal(PointConfig[ConfigLocationIdx].X,PointConfig[ConfigLocationIdx].Y);
	}
	else
	{
		/* do nothing */
	}
	if(FormReresh)
	{
		VP_ConvertData();
		VP_FormShow() ;
		/* creat save button :D */
		LCD.SetCursorLocal(PointConfig[CONFIG_TIMER_SAVE].X,PointConfig[CONFIG_TIMER_SAVE].Y) ;
		/* write tittle */
		LCD.WriteString("SAVE",4);
		/* set curcor at first */
		LCD.SetCursorLocal(PointConfig[ConfigLocationIdx].X,PointConfig[ConfigLocationIdx].Y);
	}
	else
	{
		/* do nothing */
	}
}
/***********************************************************
** Function Name: VP_SettingTime()
** Description  : The function process setting time for system
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_SettingTime(void)
{
  uint8_t  RequestDisplay = 0;
  char   tmp[5] = {0};
  uint8_t keepGoing = 0;
	/* read button 1 */
	if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
	{
	  /* request display */	
    if(TimeSettingIdx > DATETIME_BACK)
		{
			TimeSettingIdx--;
			RequestDisplay = 1;
		}			
		else
		{
			/* do nothing */
		}
	}
	else
	{
		/* do nothing */
	}
	/* read button 2 */
	if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
	{
		switch(TimeSettingIdx)
		{
			case DATETIME_BACK: 
				/* back to state menu selecting */
				mcState = MENU_STATE;
				/* disable curror here */
				LCD_DisableCurcor();
        TitleShow = TITLE_NOT_SHOW;
			break;
			case DATETIME_SET_DAY:
				keepGoing = 1;
			  /* keep setting day */
				while(keepGoing)
				{
					/* read button 1 */
					if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
					{
						/* dcrement day */
						DateTime.Day--;
						/* check day of month setting is over 31  */
						if(DateTime.Day==0)
						{
							DateTime.Day = 31;
						}
						else
						{
							/* do nothing */
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Day);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
					/* read button 2 */
					if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
					{
						/* exit loop */
						keepGoing = 0;
					}
					else
					{
						/* do nothing */
					}
					/* read button 3 */
					if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
					{
						/* increment day */
						DateTime.Day++;
						/* check day of month setting is over 31  */
						if(DateTime.Day>31)
						{
							DateTime.Day = 1;
						}
						else
						{
							/* do nothing */
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Day);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
				}
			break;
			case DATETIME_SET_MON:
				keepGoing = 1;
			  /* keep setting Month */
				while(keepGoing)
				{
					/* read button 1 */
					if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
					{
						/* decrement month */
						DateTime.Month--;
						/* check  month setting is over 12  */
						if(DateTime.Month==0)
						{
							DateTime.Month = 12;
						}
						else
						{
							/* do nothing */
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Month);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
					/* read button 2 */
					if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
					{
						/* exit loop */
						keepGoing = 0;
					}
					else
					{
						/* do nothing */
					}
					/* read button 3 */
					if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
					{
						/* increment Month */
						DateTime.Month++;
						/* check day of month setting is over 12  */
						if(DateTime.Month>12)
						{
							DateTime.Month = 1;
						}
						else
						{
							/* do nothing */
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Month);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
				}
			break;
			case DATETIME_SET_YEAR:
				keepGoing = 1;
			  /* keep setting Year */
				while(keepGoing)
				{
					/* read button 1 */
					if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
					{
						/* check day of month setting is over 12  */
						if(DateTime.Year==0)
						{
							DateTime.Year = 99;
						}
						else
						{
							/* decrement Year */
							DateTime.Year--;
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Year);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
					/* read button 2 */
					if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
					{
						/* exit loop */
						keepGoing = 0;
					}
					else
					{
						/* do nothing */
					}
					/* read button 3 */
					if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
					{
						/* increment Year */
						DateTime.Year++;
						/* check Year setting is over 12  */
						if(DateTime.Year>99)
						{
							DateTime.Year = 0;
						}
						else
						{
							/* do nothing */
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Year);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
				}
			break;		
			case DATETIME_SET_HOUR:				
				keepGoing = 1;
			  /* keep setting Hour */
				while(keepGoing)
				{
					/* read button 1 */
					if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
					{
						/* check Hour setting is over 12  */
						if(DateTime.Hour==0)
						{
							DateTime.Hour = 23;
						}
						else
						{
							/* decrement Year */
							DateTime.Hour--;
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Hour);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
					/* read button 2 */
					if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
					{
						/* exit loop */
						keepGoing = 0;
					}
					else
					{
						/* do nothing */
					}
					/* read button 3 */
					if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
					{
						/* increment Year */
						DateTime.Hour++;
						/* check Year setting is over 24  */
						if(DateTime.Hour>23)
						{
							DateTime.Hour = 0;
						}
						else
						{
							/* do nothing */
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Hour);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
				}
			break;
      case DATETIME_SET_MIN:				
				keepGoing = 1;
			  /* keep setting Min */
				while(keepGoing)
				{
					/* read button 1 */
					if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
					{
						/* check Hour setting is under 0  */
						if(DateTime.Min==0)
						{
							DateTime.Min = 59;
						}
						else
						{
							/* decrement Min */
							DateTime.Min--;
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Min);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
					/* read button 2 */
					if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
					{
						/* exit loop */
						keepGoing = 0;
					}
					else
					{
						/* do nothing */
					}
					/* read button 3 */
					if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
					{
						/* increment Min */
						DateTime.Min++;
						/* check Year setting is over 59  */
						if(DateTime.Min>59)
						{
							DateTime.Min = 0;
						}
						else
						{
							/* do nothing */
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Min);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
				}
			break;
      case DATETIME_SET_SEC:				
				keepGoing = 1;
			  /* keep setting Min */
				while(keepGoing)
				{
					/* read button 1 */
					if(BUT1_IS_PRESSED == VP_ScanButton(BUT_1))
					{
						/* check Hour setting is under 0  */
						if(DateTime.Sec==0)
						{
							DateTime.Sec = 59;
						}
						else
						{
							/* decrement Sec */
							DateTime.Sec--;
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Sec);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
					/* read button 2 */
					if(BUT2_IS_PRESSED == VP_ScanButton(BUT_2))
					{
						/* exit loop */
						keepGoing = 0;
					}
					else
					{
						/* do nothing */
					}
					/* read button 3 */
					if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
					{
						/* increment Sec */
						DateTime.Sec++;
						/* check Sec setting is over 59  */
						if(DateTime.Sec>59)
						{
							DateTime.Sec = 0;
						}
						else
						{
							/* do nothing */
						}
						/* convert to string */
						sprintf(tmp,"%02d",DateTime.Sec);
						/* display to LCD */
						LCD.WriteString(tmp,strlen(tmp));
						/* set curcor back  */
						LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
					}
					else
					{
						/* do nothing */
					}
				}
			break;      
      case DATETIME_SAVE:				
       /* clear screen */
      LCD.ClearScreen();
      Time_Adjust(Sec_Diff(1,1,1970,DateTime.Day,DateTime.Month,DateTime.Year+2000,DateTime.Hour,DateTime.Min,DateTime.Sec));
      LCD.Printf(" LUU THOI GIAN  ",16,1);
      LCD.Printf("   THANH CONG   ",16,2);      
      VP_DelayMs(1000);
      /* clear screen */
      LCD.ClearScreen();
      /* display main */
      sprintf(tmp,"BACK    %02d/%02d/%02d",DateTime.Day,DateTime.Month,DateTime.Year);
      LCD.Printf(tmp,strlen(tmp),1);
      /* convert time */
      sprintf(tmp,"SAVE    %02d:%02d:%02d",DateTime.Hour,DateTime.Min,DateTime.Sec);
      /* display to tiem to lcd */
      LCD.Printf(tmp,strlen(tmp),2);
      /* set curcor at back button */
      TimeSettingIdx = DATETIME_BACK; 
      /* request display */
      RequestDisplay = 1;
			break;      
			default: break;
		}
		
	}
	else
	{
		/* do nothing */
	}
	/* read button 4 */
	if(BUT3_IS_PRESSED == VP_ScanButton(BUT_3))
	{
	  /* request display */	
    if(TimeSettingIdx < DATETIME_SET_SEC)
		{
			TimeSettingIdx++;
			RequestDisplay = 1;
		}			
		else
		{
			/* do nothing */
		}
	}
	else
	{
		/* do nothing */
	}	
	/* check process display */
  if(RequestDisplay)
	{
		LCD.SetCursorLocal(CoordinateTime[TimeSettingIdx].X,CoordinateTime[TimeSettingIdx].Y);
	}
	else
	{
		/* do nothing */
	}
}
/***********************************************************
** Function Name: VP_FormShow()
** Description  : The function show object form to LCD
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_FormShow(void)
{
	char tmp[16] = {0};
	/* check form display */
	switch(DisplayIdx)
	{
		case DISPLAY_MAIN   : 
			if(TitleShow == TITLE_NOT_SHOW)
			{
				/* display to line one */	
				LCD.Printf(FormObject[DISPLAY_MAIN].LineOne,16,1);
				LCD.Printf(FormObject[DISPLAY_MAIN].LineSecond,16,2);
				TitleShow = TITLE_SHOWED;
			}
			else
			{
				/* do nothing */
			}
			/* display to line two */
			/* check second changed */
			if(DateTimeCurrent.Sec != DateTime.Sec)
			{
				/* copy sec to current sec */
				DateTimeCurrent.Sec = DateTime.Sec;
				/* convert to string */
				sprintf(tmp,"%02d",DateTime.Sec);
				/* set location curror of LCD */
				LCD.SetCursorLocal(10,LINE_BOT);
				/* write data display */
				LCD.WriteString(tmp,strlen(tmp));
			}
			else
			{
				/* do nothing */
			} 
			/* check minutes changed */
			if(DateTimeCurrent.Min != DateTime.Min)
			{
				/* copy sec to current sec */
				DateTimeCurrent.Min = DateTime.Min;
				/* convert to string */
				sprintf(tmp,"%02d",DateTime.Min);
				/* set location curror of LCD */
				LCD.SetCursorLocal(7,LINE_BOT);
				/* write data display */
				LCD.WriteString(tmp,strlen(tmp));
			}
			else
			{
				/* do nothing */
			}
			/* check hour changed */
			if(DateTimeCurrent.Hour != DateTime.Hour)
			{
				/* copy sec to current sec */
				DateTimeCurrent.Hour = DateTime.Hour;
				/* convert to string */
				sprintf(tmp,"%02d",DateTime.Hour);
				/* set location curror of LCD */
				LCD.SetCursorLocal(4,LINE_BOT);
				/* write data display */
				LCD.WriteString(tmp,strlen(tmp));
			}
			else
			{
				/* do nothing */
			}
		break;
		case DISPLAY_TIMER_1: 
			/* display to line one */	
			LCD.Printf(FormObject[DISPLAY_TIMER_1].LineOne,16,1);
			/* display to line two */
			LCD.Printf(FormObject[DISPLAY_TIMER_1].LineSecond,16,2);	
		break;
		case DISPLAY_TIMER_2: 
			/* display to line one */	
			LCD.Printf(FormObject[DISPLAY_TIMER_2].LineOne,16,1);
			/* display to line two */
			LCD.Printf(FormObject[DISPLAY_TIMER_2].LineSecond,16,2);						
		break;
		case DISPLAY_TIMER_3:
			/* display to line one */	
			LCD.Printf(FormObject[DISPLAY_TIMER_3].LineOne,16,1);
			/* display to line two */
			LCD.Printf(FormObject[DISPLAY_TIMER_3].LineSecond,16,2);	
		break;
		default: break;
	}
}
/***********************************************************
** Function Name: VP_ScanButton()
** Description  : The function scan and return key pressed
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
ButtonPressedType VP_ScanButton(ButtonScanType  but)
{
	uint32_t FilterTime = 0;
	ButtonPressedType returnValue = NONE_BUTTON_PRESSED;
	switch(but)
	{
		case BUT_1:
      /* read button 1 */
      if(!IO_read(BUT1))
      {
        FilterTime = VP_GlobalGetSystick();
        /* wait for release button */
        while((VP_GlobalGetSystick() - FilterTime < 500) && !IO_read(BUT1))
        {;}
        /* release button */
        returnValue = BUT1_IS_PRESSED ;			
      }
      else
      {
        /* do nothing */
      }
		break;
		case BUT_2:
			/* read button 2 */
			if(!IO_read(BUT2))
			{
				FilterTime = VP_GlobalGetSystick();
				/* wait for release button */
				while((VP_GlobalGetSystick() - FilterTime < 500) && !IO_read(BUT2))
				{;}
				/* release button */
				returnValue = BUT2_IS_PRESSED ;			
			}
			else
			{
				/* do nothing */
			}			
		break;
		case BUT_3:
				/* read button 3 */
			if(!IO_read(BUT3))
			{
				FilterTime = VP_GlobalGetSystick();
				/* wait for release button */
				while((VP_GlobalGetSystick() - FilterTime) < 500 && !IO_read(BUT3))
				{;}
						/* release button */
				returnValue = BUT3_IS_PRESSED ;		
			}
			else
			{
				/* do nothing */
			}				
		break;		
		default: break;
	}
	return returnValue;
}
/***********************************************************
** Function Name: SysTick_Handler()
** Description  : The function process system tick interrupt
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
void SysTick_Handler (void)
{
  /* decrement time delay */
  VP_DecrementTimingDelay();
	/* increment system tick  */
	VP_IncrementSystick();
	/* check LCD1 is pluged */
		/* check LCD is pluged */
	if(!IO_read(LCD_DETECT))
	{
		LCDDetected = LCD_PLUGED;
	}
	else
	{
		LCDDetected = LCD_NOT_PLUG ;
		mcState = MAIN_STATE;
	}
	/* convert data to LCD */
	VP_ConvertData() ;
}
/***********************************************************
** Function Name: VP_ConvertData()
** Description  : The function shall convert data for LCD display
** Parameter    : None
** Return       : None
** Remark       : main();
************************************************************/
static void VP_ConvertData(void)
{
	/* data for form main */
	/* line one */
  sprintf(FormObject[DISPLAY_MAIN].LineOne,"%s   %02d/%02d/%02d",DayOfWeek[DateTime.Date],DateTime.Day,DateTime.Month,DateTime.Year+2000);
	/* line second */
	sprintf(FormObject[DISPLAY_MAIN].LineSecond,"    %02d:%02d:%02d   >",DateTime.Hour,DateTime.Min,DateTime.Sec);
	/* data for form timer 1 */
	if(mcState == CONFIG_STATE)
	{
		  sprintf(FormObject[DISPLAY_TIMER_1].LineOne,"%s%s","TIMER 1      ",TimerDeEn[Timer.TimerConfigON[DisplayIdx].Enabled]);
	}
	else
	{
		sprintf(FormObject[DISPLAY_TIMER_1].LineOne,"%s","CAI DAT TIMER 1 ");
	}
		/* line second */
	if(mcState == CONFIG_STATE)
	{
		/* check current config mode */
		if(FormObject[DISPLAY_TIMER_1].CfgMode == CONFIG_TIMER_ON)
		{
			/* convert data of trigger on */
			sprintf(FormObject[DISPLAY_TIMER_1].LineSecond,"< ON  %02d:%02d SAVE",Timer.TimerConfigON[TIMER_1].Hour,Timer.TimerConfigON[TIMER_1].Min) ;
		}
		else
		{
			/* convert data of trigger off */
			sprintf(FormObject[DISPLAY_TIMER_1].LineSecond,"< OFF %02d:%02d SAVE",Timer.TimerConfigOFF[TIMER_1].Hour,Timer.TimerConfigOFF[TIMER_1].Min) ;
		}
	}
	else
	{
				/* check current config mode */
		if(FormObject[DISPLAY_TIMER_1].CfgMode == CONFIG_TIMER_ON)
		{
			/* convert data of trigger on */
			sprintf(FormObject[DISPLAY_TIMER_1].LineSecond,"< ON  %02d:%02d    >",Timer.TimerConfigON[TIMER_1].Hour,Timer.TimerConfigON[TIMER_1].Min) ;
		}
		else
		{
			/* convert data of trigger off */
			sprintf(FormObject[DISPLAY_TIMER_1].LineSecond,"< OFF %02d:%02d    >",Timer.TimerConfigOFF[TIMER_1].Hour,Timer.TimerConfigOFF[TIMER_1].Min) ;
		}
	}
	/* data for form timer 2 */
	if(mcState == CONFIG_STATE)
	{
		  sprintf(FormObject[DISPLAY_TIMER_2].LineOne,"%s%s","TIMER 2      ",TimerDeEn[Timer.TimerConfigON[DisplayIdx].Enabled]);
	}
	else
	{
		sprintf(FormObject[DISPLAY_TIMER_2].LineOne,"%s","CAI DAT TIMER 2 ");
	}
	/* line second */
	if(mcState == CONFIG_STATE)
	{
		/* check current config mode */
		if(FormObject[DISPLAY_TIMER_2].CfgMode == CONFIG_TIMER_ON)
		{
			/* convert data of trigger on */
			sprintf(FormObject[DISPLAY_TIMER_2].LineSecond,"< ON  %02d:%02d SAVE",Timer.TimerConfigON[TIMER_2].Hour,Timer.TimerConfigON[TIMER_2].Min) ;
		}
		else
		{
			/* convert data of trigger off */
			sprintf(FormObject[DISPLAY_TIMER_2].LineSecond,"< OFF %02d:%02d SAVE",Timer.TimerConfigOFF[TIMER_2].Hour,Timer.TimerConfigOFF[TIMER_2].Min) ;
		}
	}
	else
	{
				/* check current config mode */
		if(FormObject[DISPLAY_TIMER_2].CfgMode == CONFIG_TIMER_ON)
		{
			/* convert data of trigger on */
			sprintf(FormObject[DISPLAY_TIMER_2].LineSecond,"< ON  %02d:%02d    >",Timer.TimerConfigON[TIMER_2].Hour,Timer.TimerConfigON[TIMER_2].Min) ;
		}
		else
		{
			/* convert data of trigger off */
			sprintf(FormObject[DISPLAY_TIMER_2].LineSecond,"< OFF %02d:%02d    >",Timer.TimerConfigOFF[TIMER_2].Hour,Timer.TimerConfigOFF[TIMER_2].Min) ;
		}
	}
	/* data for form timer 3 */
	if(mcState == CONFIG_STATE)
	{
		  sprintf(FormObject[DISPLAY_TIMER_3].LineOne,"%s%s","TIMER 3      ",TimerDeEn[Timer.TimerConfigON[DisplayIdx].Enabled]);
	}
	else
	{
		sprintf(FormObject[DISPLAY_TIMER_3].LineOne,"%s","CAI DAT TIMER 3 ");
	}
	/* line second */
	if(mcState == CONFIG_STATE)
	{
		/* check current config mode */
		if(FormObject[DISPLAY_TIMER_3].CfgMode == CONFIG_TIMER_ON)
		{
			/* convert data of trigger on */
			sprintf(FormObject[DISPLAY_TIMER_3].LineSecond,"< ON  %02d:%02d SAVE",Timer.TimerConfigON[TIMER_3].Hour,Timer.TimerConfigON[TIMER_3].Min) ;
		}
		else
		{
			/* convert data of trigger off */
			sprintf(FormObject[DISPLAY_TIMER_3].LineSecond,"< OFF %02d:%02d SAVE",Timer.TimerConfigOFF[TIMER_3].Hour,Timer.TimerConfigOFF[TIMER_3].Min) ;
			FormObject[DISPLAY_TIMER_3].CfgMode = CONFIG_TIMER_OFF;
		}
	}
	else
	{
		/* check current config mode */
		if(FormObject[DISPLAY_TIMER_3].CfgMode == CONFIG_TIMER_ON)
		{
			/* convert data of trigger on */
			sprintf(FormObject[DISPLAY_TIMER_3].LineSecond,"< ON  %02d:%02d      ",Timer.TimerConfigON[TIMER_3].Hour,Timer.TimerConfigON[TIMER_3].Min) ;
		}
		else
		{
			/* convert data of trigger off */
			sprintf(FormObject[DISPLAY_TIMER_3].LineSecond,"< OFF %02d:%02d      ",Timer.TimerConfigOFF[TIMER_3].Hour,Timer.TimerConfigOFF[TIMER_3].Min) ;
			FormObject[DISPLAY_TIMER_3].CfgMode = CONFIG_TIMER_OFF;
		}
	}	
}
/********************** End Of File *************************/
/* VietComp Electronics Copyright  2013                     */
/************************************************************/
